const Payment = require('./payment');
const Operators = require('./operators');
const RepairType = require('./repair-type');


module.exports = (sequelize, DataTypes) => {
    const repair_service = sequelize.define(
        'repair_service',
        {
          id: {
            type: DataTypes.INTEGER(11),
            primaryKey: true,
            autoIncrement: true
          },
          operateTime: {
            type: DataTypes.STRING,
            allowNull: false,
            field: "operate-time"
          },
          detailAffairs: {
              type: DataTypes.STRING,
              field: "detail-affairs"
          },
          price: {
            type: DataTypes.DECIMAL,
          },
          paymentId: {
              type: DataTypes.INTEGER,
              field: "payment-id",
              allowNull: false,
              references: {
                  modal: Payment,
                  key: 'id'
              }
          },
          isDoorToDoor: {//1 true; 0 false
              type: DataTypes.INTEGER,
              field: "is-door-to-door"
          },
          operatorId: {
              type: DataTypes.INTEGER,
              field: "operator-id",
              allowNull: false,
              references: {
                  modal: Operators,
                  key: 'id'
              }
          },
          repairId: {
              type: DataTypes.INTEGER,
              field: "repair-id",
              allowNull: false,
              references: {//指定外键
                  modal: RepairType,
                  key: 'id'
              }
          }
        },
        {
          tableName: 'repair-service',
          timestamps: false,//使用事务时，出现Unknown column 'created_at' in 'field list'，原因是sequelize默认会自动为其添加 createdAt 和updatedAt column值，添加timestamp处理
        },
    )
    repair_service.associate = (models) => {//定义外键关联，与customers model的定义匹配。这里，foreignKey定义的是customers与electric_sale外键关联的字段，sourceKey才是定义electric_sale自己外键是哪个字段的。不定义sourceKey的话，系统默认与主键关联。。。坑爹，api上没看见hasMany方法的options里面有这个sourceKey的配置，我可能看了一个假api。。。
        repair_service.hasMany(
            models.payment,
            {foreignKey: 'id',
            sourceKey: 'payment-id'}
        );
        repair_service.hasMany(
            models.operators,
            {foreignKey: 'id',
            sourceKey: 'operator-id'}
        );
        repair_service.hasMany(
            models.repair_type,
            {foreignKey: 'id',
            sourceKey: 'repair-id'}
        );
    }
    return repair_service;
};